<?php

class MetaModel{
protected $project_id;

public function __construct($project_id){
	$this->project_id = $project_id;
}

public function DumpCrudSets(){
	$q = Doctrine_Query::create()
	->from('Crudsets cs')
	->where('project_id = ?',$this->project_id);

	$ar_crudsets = $q->fetchArray();

	echo "<pre>";
	print_r($ar_crudsets);
	echo "</pre>";

}

public function GetProjectData(){
	$q = Doctrine_Query::create()
	->from('Projects pro')
	->where('project_id = ?',$this->project_id);
	$ar_projects = $q->fetchArray();
	return $ar_projects[0];
	
}

	public function LoadDatabaseTables(){
		$manager = Doctrine_Manager::getInstance();
		$ar_project_info = $this->GetProjectData();

		$main_connection_name = $manager->getCurrentConnection()->getName();

		$engine = 'mysql';
		$host = $ar_project_info['project_server'];
		$db = $ar_project_info['project_db'];
		$user = $ar_project_info['project_login'];
		$password = $ar_project_info['project_pass'];
		$port = '3306';

		$dsn2 = $engine . ':' . '//' . $user . ':' . $password . '@' . $host . ':' . $port . '/' . $db;

		$subdb = Doctrine_Manager::connection($dsn2,'subdb');

		$manager->setCurrentConnection('subdb');

		Doctrine::generateYamlFromDb('subdbschema.yml', array('subdb'));

		$newyaml = new sfYamlParser();
		$ar_subdb_info = $newyaml->parse(file_get_contents('subdbschema.yml'));

		$manager->setCurrentConnection('maindb');

		Doctrine::loadModels('models');

		$output .= "<table>";
		foreach($ar_subdb_info AS $table_name => $ar_table_info){
			$output .= "<tr><td>$table_name</td>";
			$output .= "<td><table>";
			$q_isthere = Doctrine_Query::create()
			->from('Tablesources ts') 
			->where('table_name = ?', $table_name)
			->andWhere('project_id = ?', $ar_project_info['project_id']);
			$doit = false;
			if($q_isthere->count() == 0){
			
				$newtable = new Tablesources();
				$newtable->table_name = $table_name;
				$newtable->project_id = $ar_project_info['project_id'];
				$newtable->table_alias = $table_name;
				$newtable->table_filter = 1;
				$newtable->save();
				$owner_table_id = $newtable->tablesource_id;
				$doit = true;
			}
			foreach($ar_table_info['columns'] AS $column_name => $ar_column_info){
				if($doit){
					$newvariable = new Variables();
					$newvariable->variable_name = $column_name;
					$newvariable->type = $ar_column_info['type'];
					if($ar_column_info['primary'] != 1){
						$is_key = 0;
					}else{
						$is_key = 1;
					}
					$newvariable->is_key = $is_key;
					$newvariable->source_id = $owner_table_id;
					$newvariable->def_show_text = '';
					$newvariable->save();
				}
				$output .= "<tr><td>$column_name</td><td></td><td>{$ar_column_info['type']}</td><td>{$ar_column_info['primary']}</td><td>{$ar_column_info['autoincrement']}</td></tr>";
			}
			$output .= "</table></td></tr>";
		}
		$output .= "</table>";
		
		echo "Output is <br />" . $output;
	}

public function AddTableVarsToSet($table_id, $set_type = 'inline_edit'){

	$q = Doctrine_Query::create()
	->from('Tablesources t')->where('tablesource_id = ?',$table_id);
	$table = $q->execute()->getFirst();
	
	$set = new Crudsets();
	$set->cs_name = $table->table_alias;
	$set->project_id = $this->project_id;
	$set->main_table = $table->table_id;
	//$set->cs_type = $set_type;
	$set->subdirectory = $table->table_alias . '/';
	$set->save();

	$q = Doctrine_Query::create()
	->from('Variables var')
	->where('source_id = ?',$table_id);

	$ar_variables = $q->fetchArray();

	
	foreach($ar_variables AS $variable){
		$column = New Crudcols();
		$column->crudset_id = $set->crudset_id;
		$column->variable_id = $variable['variable_id'];
		$column->is_key = $variable['is_key'];
		$column->shown_in_list = 1;
		($variable['is_key'] == 1)?$column->edit_in_list = 0:$column->edit_in_list = 1;
		$column->edit_type = 'text';
		$column->filter = 1;
		$column->showfilter = 0;
		$column->col_order = 0;
		$column->makefunction = '';
		$column->save();
		
	}
	
	
}	
	
	
}


?>